﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Yglab.Models;
using Yglab.MyLib;

namespace Yglab.Controllers
{
    public class UserController : Controller
    {
        private YgContext conn = new YgContext();

        public ActionResult Register()
        {
            return View();
        }

        [HttpGet]
        public ActionResult Login()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Login(string Email, string Password)
        {
            List<User> userList = conn.Users.Where(user => user.Email == Email).ToList();

            if (userList.Count != 1)
                return RedirectToAction("Login");

            if(! userList.ElementAt(0).Password.Equals(Password))
                return RedirectToAction("Login");

            MySingletonSession.Current.CurrentUser = userList.ElementAt(0);
            return RedirectToAction("Notifications", "User");
        }

        public ActionResult Logout()
        {
            MySingletonSession.Current.CurrentUser = null;
            return RedirectToAction("Index", "Home");
        }

        public ActionResult UserProfile()
        {
            return View();
        }

        public ActionResult Notifications() // login olunca ilk açılacak olan sayfa bu.
        {
            User u = MySingletonSession.Current.CurrentUser;
            List<Notification> notifs = conn.Notifications.Include("User").Include("Invitation").Include("Invitation.Journal").
                Include("Versionn").Include("Versionn.Article").Include("Versionn.Article.Journal").Include("Invitation.Versionn").
                Include("Invitation.Versionn.Article.Journal").
                Where(n => n.User.Id == u.Id).ToList();

            /*List<Gui_Notification> gui_notifs = new List<Gui_Notification>();
            foreach (Notification notif in notifs)
            {
                Journal j = conn.Journals.FirstOrDefault(j => j.);
            }*/
            return View(notifs);
        }

        // yazdığı makaleler: durumuna göre alt kategoride linkleri var bunun.
        // editörlüğünü yaptığı makaleler. 
        // hakemlik yaptığı makaleler 
        public ActionResult Articles()
        {
            // görüntüleyenin kim olduğuna göre yukardaki listenin hangilerinin görüntülenebileceği değişir.
            return View();
        }
    }
}
